package org.jivesoftware.smack.packet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.jivesoftware.smack.util.StringUtils;

public class Message extends Packet
{
  private final Set<Body> bodies;
  private String language;
  private final Set<Subject> subjects;
  private String thread;
  private Type type = Type.normal;

  public Message()
  {
    this.thread = null;
    this.subjects = new HashSet();
    this.bodies = new HashSet();
  }

  public Message(String paramString)
  {
    this.thread = null;
    this.subjects = new HashSet();
    this.bodies = new HashSet();
    setTo(paramString);
  }

  public Message(String paramString, Type paramType)
  {
    this.thread = null;
    this.subjects = new HashSet();
    this.bodies = new HashSet();
    setTo(paramString);
    this.type = paramType;
  }

  private String determineLanguage(String paramString)
  {
    String str;
    if ("".equals(paramString))
      paramString = null;
    if ((paramString == null) && (this.language != null))
      str = this.language;
    while (true)
    {
      while (true)
      {
        return str;
        if (paramString != null)
          break;
        str = getDefaultLanguage();
      }
      str = paramString;
    }
  }

  private Body getMessageBody(String paramString)
  {
    Object localObject;
    String str = determineLanguage(paramString);
    Iterator localIterator = this.bodies.iterator();
    if (!(localIterator.hasNext()))
      localObject = null;
    while (true)
    {
      Body localBody;
      do
      {
        return localObject;
        localBody = (Body)localIterator.next();
      }
      while (!(str.equals(Body.access$1(localBody))));
      localObject = localBody;
    }
  }

  private Subject getMessageSubject(String paramString)
  {
    Object localObject;
    String str = determineLanguage(paramString);
    Iterator localIterator = this.subjects.iterator();
    if (!(localIterator.hasNext()))
      localObject = null;
    while (true)
    {
      Subject localSubject;
      do
      {
        return localObject;
        localSubject = (Subject)localIterator.next();
      }
      while (!(str.equals(Subject.access$1(localSubject))));
      localObject = localSubject;
    }
  }

  public Body addBody(String paramString1, String paramString2)
  {
    Body localBody = new Body(determineLanguage(paramString1), paramString2, null);
    this.bodies.add(localBody);
    return localBody;
  }

  public Subject addSubject(String paramString1, String paramString2)
  {
    Subject localSubject = new Subject(determineLanguage(paramString1), paramString2, null);
    this.subjects.add(localSubject);
    return localSubject;
  }

  public boolean equals(Object paramObject)
  {
    int i;
    if (this == paramObject)
      i = 1;
    while (true)
    {
      while (true)
      {
        Message localMessage;
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                return i;
                if ((paramObject != null) && (getClass() == paramObject.getClass()))
                  break;
                i = 0;
              }
              localMessage = (Message)paramObject;
              if (super.equals(localMessage))
                break;
              i = 0;
            }
            if ((this.bodies.size() == localMessage.bodies.size()) && (this.bodies.containsAll(localMessage.bodies)))
              break;
            i = 0;
          }
          if (this.language != null)
            if (this.language.equals(localMessage.language))
              break label122;
          do
            while (true)
              i = 0;
          while (localMessage.language != null);
          label122: if ((this.subjects.size() == localMessage.subjects.size()) && (this.subjects.containsAll(localMessage.subjects)))
            break;
          i = 0;
        }
        if (this.thread != null)
          if (this.thread.equals(localMessage.thread))
            break label197;
        do
          while (true)
            i = 0;
        while (localMessage.thread != null);
        label197: if (this.type != localMessage.type)
          break;
        i = 1;
      }
      i = 0;
    }
  }

  public Collection<Body> getBodies()
  {
    return Collections.unmodifiableCollection(this.bodies);
  }

  public String getBody()
  {
    return getBody(null);
  }

  public String getBody(String paramString)
  {
    String str;
    Body localBody = getMessageBody(paramString);
    if (localBody == null)
      str = null;
    while (true)
    {
      return str;
      str = Body.access$0(localBody);
    }
  }

  public Collection<String> getBodyLanguages()
  {
    Body localBody1 = getMessageBody(null);
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.bodies.iterator();
    while (true)
    {
      Body localBody2;
      do
      {
        if (!(localIterator.hasNext()))
          return Collections.unmodifiableCollection(localArrayList);
        localBody2 = (Body)localIterator.next();
      }
      while (localBody2.equals(localBody1));
      localArrayList.add(Body.access$1(localBody2));
    }
  }

  public String getLanguage()
  {
    return this.language;
  }

  public String getSubject()
  {
    return getSubject(null);
  }

  public String getSubject(String paramString)
  {
    String str;
    Subject localSubject = getMessageSubject(paramString);
    if (localSubject == null)
      str = null;
    while (true)
    {
      return str;
      str = Subject.access$0(localSubject);
    }
  }

  public Collection<String> getSubjectLanguages()
  {
    Subject localSubject1 = getMessageSubject(null);
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.subjects.iterator();
    while (true)
    {
      Subject localSubject2;
      do
      {
        if (!(localIterator.hasNext()))
          return Collections.unmodifiableCollection(localArrayList);
        localSubject2 = (Subject)localIterator.next();
      }
      while (localSubject2.equals(localSubject1));
      localArrayList.add(Subject.access$1(localSubject2));
    }
  }

  public Collection<Subject> getSubjects()
  {
    return Collections.unmodifiableCollection(this.subjects);
  }

  public String getThread()
  {
    return this.thread;
  }

  public Type getType()
  {
    return this.type;
  }

  public int hashCode()
  {
    int i;
    int k;
    int l;
    int i1;
    if (this.type != null)
    {
      i = this.type.hashCode();
      int j = 31 * (i * 31 + this.subjects.hashCode());
      if (this.thread == null)
        break label96;
      k = this.thread.hashCode();
      l = 31 * (j + k);
      if (this.language == null)
        break label101;
      i1 = this.language.hashCode();
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return (31 * (l + i1) + this.bodies.hashCode());
          i = 0;
        }
        label96: k = 0;
      }
      label101: i1 = 0;
    }
  }

  public boolean removeBody(String paramString)
  {
    boolean bool;
    String str = determineLanguage(paramString);
    Iterator localIterator = this.bodies.iterator();
    if (!(localIterator.hasNext()))
      bool = false;
    while (true)
    {
      Body localBody;
      do
      {
        return bool;
        localBody = (Body)localIterator.next();
      }
      while (!(str.equals(Body.access$1(localBody))));
      bool = this.bodies.remove(localBody);
    }
  }

  public boolean removeBody(Body paramBody)
  {
    return this.bodies.remove(paramBody);
  }

  public boolean removeSubject(String paramString)
  {
    boolean bool;
    String str = determineLanguage(paramString);
    Iterator localIterator = this.subjects.iterator();
    if (!(localIterator.hasNext()))
      bool = false;
    while (true)
    {
      Subject localSubject;
      do
      {
        return bool;
        localSubject = (Subject)localIterator.next();
      }
      while (!(str.equals(Subject.access$1(localSubject))));
      bool = this.subjects.remove(localSubject);
    }
  }

  public boolean removeSubject(Subject paramSubject)
  {
    return this.subjects.remove(paramSubject);
  }

  public void setBody(String paramString)
  {
    if (paramString == null)
      removeBody("");
    while (true)
    {
      return;
      addBody(null, paramString);
    }
  }

  public void setLanguage(String paramString)
  {
    this.language = paramString;
  }

  public void setSubject(String paramString)
  {
    if (paramString == null)
      removeSubject("");
    while (true)
    {
      return;
      addSubject(null, paramString);
    }
  }

  public void setThread(String paramString)
  {
    this.thread = paramString;
  }

  public void setType(Type paramType)
  {
    if (paramType == null)
      throw new IllegalArgumentException("Type cannot be null.");
    this.type = paramType;
  }

  public String toXML()
  {
    Body localBody1;
    Iterator localIterator2;
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<message");
    if (getXmlns() != null)
      localStringBuilder.append(" xmlns=\"").append(getXmlns()).append("\"");
    if (this.language != null)
      localStringBuilder.append(" xml:lang=\"").append(getLanguage()).append("\"");
    if (getPacketID() != null)
      localStringBuilder.append(" id=\"").append(getPacketID()).append("\"");
    if (getTo() != null)
      localStringBuilder.append(" to=\"").append(StringUtils.escapeForXML(getTo())).append("\"");
    if (getFrom() != null)
      localStringBuilder.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\"");
    if (this.type != Type.normal)
      localStringBuilder.append(" type=\"").append(this.type).append("\"");
    localStringBuilder.append(">");
    Subject localSubject1 = getMessageSubject(null);
    if (localSubject1 != null)
    {
      localStringBuilder.append("<subject>").append(StringUtils.escapeForXML(localSubject1.getSubject()));
      localStringBuilder.append("</subject>");
    }
    Iterator localIterator1 = getSubjects().iterator();
    if (!(localIterator1.hasNext()))
    {
      localBody1 = getMessageBody(null);
      if (localBody1 != null)
        localStringBuilder.append("<body>").append(StringUtils.escapeForXML(Body.access$0(localBody1))).append("</body>");
      localIterator2 = getBodies().iterator();
    }
    while (true)
    {
      Body localBody2;
      do
      {
        while (!(localIterator2.hasNext()))
        {
          if (this.thread != null)
            localStringBuilder.append("<thread>").append(this.thread).append("</thread>");
          if (this.type == Type.error)
          {
            XMPPError localXMPPError = getError();
            if (localXMPPError != null)
              localStringBuilder.append(localXMPPError.toXML());
          }
          localStringBuilder.append(getExtensionsXML());
          localStringBuilder.append("</message>");
          return localStringBuilder.toString();
          Subject localSubject2 = (Subject)localIterator1.next();
          localStringBuilder.append("<subject xml:lang=\"" + localSubject2.getLanguage() + "\">");
          localStringBuilder.append(StringUtils.escapeForXML(localSubject2.getSubject()));
          localStringBuilder.append("</subject>");
        }
        localBody2 = (Body)localIterator2.next();
      }
      while (localBody2.equals(localBody1));
      localStringBuilder.append("<body xml:lang=\"").append(localBody2.getLanguage()).append("\">");
      localStringBuilder.append(StringUtils.escapeForXML(localBody2.getMessage()));
      localStringBuilder.append("</body>");
    }
  }

  public static class Body
  {
    private String language;
    private String message;

    private Body(String paramString1, String paramString2)
    {
      if (paramString1 == null)
        throw new NullPointerException("Language cannot be null.");
      if (paramString2 == null)
        throw new NullPointerException("Message cannot be null.");
      this.language = paramString1;
      this.message = paramString2;
    }

    public boolean equals(Object paramObject)
    {
      boolean bool;
      if (this == paramObject)
        bool = true;
      while (true)
      {
        while (true)
        {
          return bool;
          if ((paramObject != null) && (super.getClass() == paramObject.getClass()))
            break;
          bool = false;
        }
        Body localBody = (Body)paramObject;
        if (this.language != null)
          if (this.language.equals(localBody.language))
            break label67;
        do
          while (true)
            bool = false;
        while (localBody.language != null);
        label67: bool = this.message.equals(localBody.message);
      }
    }

    public String getLanguage()
    {
      return this.language;
    }

    public String getMessage()
    {
      return this.message;
    }

    public int hashCode()
    {
      int j;
      int i = 31 * this.message.hashCode();
      if (this.language != null)
        j = this.language.hashCode();
      while (true)
      {
        return (i + j);
        j = 0;
      }
    }
  }

  public static class Subject
  {
    private String language;
    private String subject;

    private Subject(String paramString1, String paramString2)
    {
      if (paramString1 == null)
        throw new NullPointerException("Language cannot be null.");
      if (paramString2 == null)
        throw new NullPointerException("Subject cannot be null.");
      this.language = paramString1;
      this.subject = paramString2;
    }

    public boolean equals(Object paramObject)
    {
      boolean bool;
      if (this == paramObject)
        bool = true;
      while (true)
      {
        Subject localSubject;
        while (true)
        {
          while (true)
          {
            return bool;
            if ((paramObject != null) && (super.getClass() == paramObject.getClass()))
              break;
            bool = false;
          }
          localSubject = (Subject)paramObject;
          if (this.language.equals(localSubject.language))
            break;
          bool = false;
        }
        bool = this.subject.equals(localSubject.subject);
      }
    }

    public String getLanguage()
    {
      return this.language;
    }

    public String getSubject()
    {
      return this.subject;
    }

    public int hashCode()
    {
      return (31 * this.subject.hashCode() + this.language.hashCode());
    }
  }

  public static enum Type
  {
    static
    {
      chat = new Type("chat", 1);
      groupchat = new Type("groupchat", 2);
      headline = new Type("headline", 3);
      error = new Type("error", 4);
      Type[] arrayOfType = new Type[5];
      arrayOfType[0] = normal;
      arrayOfType[1] = chat;
      arrayOfType[2] = groupchat;
      arrayOfType[3] = headline;
      arrayOfType[4] = error;
      ENUM$VALUES = arrayOfType;
    }

    public static Type fromString(String paramString)
    {
      Type localType1;
      Type localType2;
      try
      {
        localType2 = valueOf(paramString);
        localType1 = localType2;
        return localType1;
      }
      catch (Exception localException)
      {
        localType1 = normal;
      }
    }
  }
}